import { app } from "../experience/utils/F7.js";
import Db from "../experience/db/Db.js";
import Api from "../experience/api/Api.js";
import Drawer from "../experience/drawer/Drawer.js";
import Utils from "../experience/utils/Utils.js";
import Algorithm from "../experience/utils/Algorithm.js";
import { CONF} from "../configure/Assets.js";

import Coordinate from "../component/other/Coordinate.js";
import Uploader from "../component/other/Uploader.js";
import Layer from "../component/piece/Layer.js";
import Fault from "../component/fault/Fault.js";
import Sensor from "../component/sensor/Sensor.js";
import Warning from "../component/warning/Warning.js";
import Selector from "../component/other/Selector.js";
import Manager from "../component/tunnel/Manager.js";
import Message from "../component/other/Message.js";
import Water from "../component/fault/Water.js";
import Fissure from "../component/fault/Fissure.js";

const $$ = Dom7;

class Skuld {
    static instance;

    constructor() {
        if (Skuld.instance) {
            return Skuld.instance;
        }
        Skuld.instance = this;

        this.db = new Db();
        this.api = new Api();
        this.utils = new Utils();
        this.algo = new Algorithm();
        this.drawer = new Drawer();

        new Coordinate();

        new Uploader();
        new Layer();
        // new Inter();
        new Selector();
        new Message();

        this.tunnel = new Manager();
        this.fault = new Fault();
        this.sensor = new Sensor();
        this.warning = new Warning();
        this.water = new Water();
        this.fissure = new Fissure();

        this.init().then(async () => {
            console.log("Walküre Fly...");
        });
    }

    async refresh() {
        // 在地图上刷新所有巷道
        await this.fault.refresh();

        // 在地图上刷新所有预警区域
        await this.warning.refresh();

        // 在地图上描画所有巷道
        await this.tunnel.refresh();

        // 在地图上描画富水区
        await this.water.refresh();

        // 在地图上描画裂隙带
        await this.fissure.refresh();

        // 在地图上刷新传感器
        this.sensor.socket();
    }

    // 全局初始化
    async init() {
        // 获取配置用的code信息
        const code = await this.api.getCode();
        this.utils.setLocalStorage('code', code.data);

        // 全局pannel关闭
        $$("#pannel-close").on("click", () => {
            $$("#pannel").hide()
        })

        // 判断本地是否已经有煤矿信息了
        const config = this.algo.getConfig();
        if (config === null) {
            const metas = await this.api.getMetas(CONF.MINE_CODE);
            if (metas.code === 0 && metas.data.length > 0) {
                const meta_id = metas.data[0].meta_id;
                app.dialog.preloader("正在获取矿图...");
                const data = await this.api.getVector(meta_id);
                if (data.code === 0) {
                    // 初始化config信息
                    this.drawer.init(data.data.meta, data.data.layers);
                    // 将从网络上取下来的数据存入数据库
                    this.db.init(data.data).then(() => {
                        // 从本地提取数据
                        this.drawer.location().then(async () => {
                            app.dialog.close();
                            await this.refresh();
                            const json = { from: "tank", type: "loadMap" };
                            window.parent.postMessage(JSON.stringify(json), '*');
                        })
                    })
                }
            }
        } else {
            // 从本地获取数据
            await this.drawer.location();
            await this.refresh();
            const json = { from: "tank", type: "loadMap" };
            window.parent.postMessage(JSON.stringify(json), '*');
        }
    }
}

new Skuld();